﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace PicStore.Common.Email
{
    public class UEmail
    {
        #region  Mail serveive properties
        public string MailFrom { get; set; }
        public string MailFromDisplayName { get; set; }
        public string MailTo { get; set; }
        public string MailSubject { get; set; }
        public string MailBody { get; set; }
        public List<string> MailAttachmentUrlList { get; set; }
        public List<string> MailCCList { get; set; }
        public bool IsHtmlMail { get; set; }
        public Encoding MailEncode { get; set; }
        public int MailSendTimeOut { get; set; }
        public string MailReply { get; set; }
        #endregion

        public UEmail()
        {
            MailFrom = ConfigurationManager.AppSettings["EmailFromAddress"];
            MailFromDisplayName = "Eland Picture Management System";
            MailTo = ConfigurationManager.AppSettings["AdminEmail"];
            MailSubject = "Eland Picture Management System";
            MailBody = "Testing Email";
            IsHtmlMail = false;
            MailEncode = Encoding.Default;
            MailSendTimeOut = 20000;
        }

        public void SendDirectly()
        {
            MailMessage mailMsg = new MailMessage(new MailAddress(MailFrom, MailFromDisplayName), new MailAddress(MailTo))
            {
                Subject = MailSubject,
                SubjectEncoding = MailEncode,
                Body = MailBody
            };
            if (!string.IsNullOrEmpty(MailReply))
            {
                mailMsg.ReplyTo = new MailAddress(MailReply);
            }
            mailMsg.BodyEncoding = MailEncode;
            if (MailAttachmentUrlList != null)
            {
                foreach (string AttachmentUrl in MailAttachmentUrlList)
                {
                    mailMsg.Attachments.Add(new Attachment(AttachmentUrl));
                }
            }
            if (MailCCList != null && MailCCList.Count > 0)
            {
                foreach (string strMail in MailCCList)
                {
                    mailMsg.CC.Add(new MailAddress(strMail));
                }
            }

            mailMsg.IsBodyHtml = true;
            mailMsg.BodyEncoding = Encoding.UTF8;
            SmtpClient smtpService = new SmtpClient { Timeout = MailSendTimeOut };
            smtpService.Send(mailMsg);
        }
    }
}
